class Solution {
public:
    int minimizeMax(vector<int>& nums, int p) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        int left = 0;
        int right = nums.back() - nums.front();
        while (left < right) {
            int mid = left + (right - left) / 2;
            int count = 0;
            int i = 0;
            while (i < n - 1) {
                if (nums[i + 1] - nums[i] <= mid) {
                    count++;
                    i += 2;
                }
                else {
                    i++;
                }
                if (count >= p) {
                    break;
                }
            }
            if (count >= p) {
                right = mid;
            }
            else {
                left = mid + 1;
            }
        }
        return left;
    }
};